<?php

		class Ref extends CI_Controller {
		
				function __construct(){
						parent::__construct();
				}
				
				function index() {
						die('index');
				}
				
				function _remap($ref) {
						$lang = $this->_resolve_lang();
						if($ref && is_numeric($ref))
						{
								$this->_resolve_referrer($ref);
						}						
						
						$uri = $this->uri->uri_to_assoc(3);
						// print_r($uri);
						// die();
						if(isset($uri['op']))
						{
								if($uri['op']=='cust')
								{
										$token 		= @$uri['token'];
									    $afflink 	= new Afflink();
										$afflink->where('token',trim($token))->get(1);
										if($afflink->exists() && $afflink->dest)
										{
												redirect($afflink->dest);
										}
										else
										{
												redirect(base_url());
										}
										die();
								}
								if($uri['op']=='regp')
								{
										redirect(base_url() . 'users/register_partner/');
										die();
								}
								
								$link = new Ref_link();
								$link->where('id', (int)$uri['op'])->get(1);
								if($link->url && $link->url!='')
								{
										redirect($link->url);
								}
								else
								{
										redirect(base_url());
								}
								die();
						
								// if($uri['op']=='hm')
								// {
										// redirect( 'http://www.goldermarkets.com/' );
								// }
								// elseif($uri['op']=='sc')
								// {
										// redirect( 'http://www.goldermarkets.org/index.php/en/trading-accounts/trading-conditions' );
								// }
								// elseif($uri['op']=='mt4')
								// {
										// redirect( 'http://www.goldermarkets.com/index.php/en/trading-platforms/meta-trader' );
								// }
								// elseif($uri['op']=='mrt')
								// {
										// redirect( 'http://www.goldermarkets.com/index.php/en/trading-platforms/mirror-trader' );
								// }
								// elseif($uri['op']=='fxtb')
								// {
										// redirect( 'http://www.goldermarkets.com/index.php/en/trading-platforms/fxstat-tradebook' );
								// }
								// elseif($uri['op']=='fxed')
								// {
										// redirect( 'http://www.goldermarkets.com/index.php/en/forex-education/forex-essentials' );
								// }
								// elseif($uri['op']=='popp')
								// {
										// redirect( 'http://www.goldermarkets.com/index.php/en/golder-markets/partnership-opportunities' );
								// }
								// elseif($uri['op']=='regp')
								// {
										// redirect( 'http://my.goldermarkets.co.uk/users/register_partner' );
								// }
								// elseif($uri['op']=='lv')
								// {
										// redirect( 'http://my.goldermarkets.co.uk/users/register_live/' . (isset($uri['lang'])?"lang/{$uri['lang']}/":'') );
								// }
								// elseif($uri['op']=='dm')
								// {
										// redirect( 'http://my.goldermarkets.co.uk/users/register_demo/' . (isset($uri['lang'])?"lang/{$uri['lang']}/":'') );
								// }
								// elseif($uri['op']=='cust')
								// {
										// $token 		= @$uri['token'];
									    // $afflink 	= new Afflink();
										// $afflink->where('token',trim($token))->get(1);
										// if($afflink->exists() && $afflink->dest)
										// {
												// redirect($afflink->dest);
										// }
										// else
										// {
												// redirect('http://www.goldermarkets.com/');
										// }
								// }
								// else
								// {
										// redirect( 'http://www.goldermarkets.com/' );
								// }
						}
						else
						{
								redirect(base_url());
						}
				}
				
				function _resolve_lang(){
						$langs = array('en','fr');
						$lang  = '';
						for($i=3;$i<$this->uri->total_segments();$i++)
						{
								if($this->uri->segment($i)=='lang' && $this->uri->segment($i+1) && in_array($this->uri->segment($i+1),$langs))
								{
										$lang = $this->uri->segment($i+1);
										break;
								}
						}
						if(!$lang && $this->session->userdata('sess_lang'))
						{
								$lang = $this->session->userdata('sess_lang');
						}
						if( !in_array($lang,$langs) ) $lang = $langs[0];
						if($lang) $this->session->set_userdata('sess_lang', $lang);
						return $lang;
				}
				
				function _resolve_referrer($ref=NULL){
						$this->load->helper('cookie');
						if(!$ref)
						{
								for($i=3;$i<$this->uri->total_segments();$i++)
								{
										if( $this->uri->segment($i)=='ref' && $this->uri->segment($i+1) )
										{
												$ref = (int)$this->uri->segment($i);
												break;
										}
								}
						}
						if($ref>0)
						{
								$referer = new User();
								$referer->where('id',(int)$ref)->get(1);
								if( $referer->exists() )
								{
										$this->load->library('encrypt');
										$opts = read_options('aff_ttl', 'OBJECT');
										$cookie = array(
											'name'   => '__rid',
											'value'  => $this->encrypt->encode($ref),
											'expire' => $opts->aff_ttl ? $opts->aff_ttl :2678400,
											'domain' => '.' . parse_url(base_url(), PHP_URL_HOST),
											'path'   => '/'
										);
										$this->input->set_cookie($cookie); 
										return $ref;
								}
						}
						elseif($this->input->cookie('__rid'))
						{
								$ref = (int)$this->encrypt->decode($this->input->cookie('__rid'));
						}
						return $ref;
				}
		}

?>